//
//  ProgressView.swift
//  HuaYun
//
//  Created by enghou on 2018/11/21.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit
import CoreGraphics


class ProgressView: UIView {
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.addSubview(colorlayer)
        colorlayer.layer.masksToBounds = true
        colorlayer.layer.cornerRadius = 0.5 * self.height
        colorlayer.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0, height: self.height))
    }
    var _percent: Double = 0
    
    var percent: Double {
        get{
            return _percent
        }
        set{
            _percent = newValue
            DispatchQueue.main.async {
                
                if newValue > 0 {
                    if newValue <= 0.3 {
                        self.colorlayer.backgroundColor = UIColor.hex("eb2439")
                    } else if newValue <= 0.6{
                        self.colorlayer.backgroundColor = UIColor.hex("ff9b00")
                    } else if newValue <= 0.8 {
                        self.colorlayer.backgroundColor = UIColor.hex("0087ff")
                    } else {
                        self.colorlayer.backgroundColor = UIColor.hex("19b960")
                    }
                    UIView.beginAnimations("anim", context: nil)
                    UIView.setAnimationDuration(TimeInterval.init(0.25))
                    self.colorlayer.width = self.width * CGFloat(newValue)
                    UIView.commitAnimations()
                } else {
                    self.colorlayer.width = self.width * CGFloat(newValue)
                }
            }
            
        }
    }
    
    private var colorlayer: UIView = UIView()

}
